; Exercise 1.7 a
;
; Q. The good-enough? test used in computing square roots will not be very
; eﬀective for ﬁnding the square roots of very small numbers. Also, in real
; computers, arithmetic operations are almost always performed with limited
; precision. This makes our test inadequate for very large numbers. Explain
; these statements, with examples showing how the test fails for small and
; large numbers.

; A. For very small numbers, what happens is that the sqrt is as good as the
; constant used in good-enough?. For example:
; (sqrt 1e-20) yields 0.03125. If you (square 0.03125) you're gonna get
; 0.0009, which is ~= 0.001, the same value of the constant used in
; good-enough?. Now, if you improve the precision of good-enough? by one order
; of magnitude, you're gonna get a different answer.
; For very large numbers, the problem is that you may never achieve the
; desired precision, resulting in an infinite loop. In chicken scheme 3.4,
; that happens for 3e300, for instance. (sqrt 3e300 is an infinite loop)

(define (improve guess x)
  (average guess (/ x guess)))

(define (average x y)
  (/ (+ x y) 2))

(define (square x)
  (* x x))

(define (good-enough? guess x)
  (< (abs (- (square guess) x)) 0.001))

(define (sqrt x)
  (sqrt-iter 1.0 x))

(define (sqrt-iter guess x)
  (if (good-enough? guess x)
    guess
    (sqrt-iter (improve guess x)
                        x)))


